package io.gitee.kotle.demo.doc

import android.content.Context
import android.os.Bundle
import io.gitee.kotle.android.adapter.refreshList
import io.gitee.kotle.android.mvvm.binding.MvvmBindingFragment
import io.gitee.kotle.android.utils.startFragment
import io.gitee.kotle.demo.databinding.FragmentDemoBinding
import io.gitee.kotle.android.delegate.viewModelDelegate
import io.gitee.kotle.android.utils.doOnSuccess

class DemoFragment : MvvmBindingFragment<FragmentDemoBinding>() {
    companion object {
        fun start(context: Context) {
            context.startFragment<DemoFragment>()
        }
    }

    /**
     * viewModel
     */
    private val demoViewModel by viewModelDelegate<DemoViewModel>()

    /**
     * 图片适配器
     */
    private val picAdapter = DemoHolder.createAdapter()


    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.root.adapter = picAdapter
    }

    override fun initObserverLiveData() {
        super.initObserverLiveData()
        //注册liveData
        demoViewModel.picData.doOnSuccess(viewLifecycleOwner) {
            //图片回调，显示在recyclerView上
            picAdapter.refreshList(it)
        }
    }

    override fun initData() {
        super.initData()
    }
}